<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Visiting
 *
 * @author wupeng
 */
include_once 'ApiAbstract.php';
include_once 'Rechange.php';
include_once 'Waybill.php';

class Visiting extends ApiAbstract {

    //put your code here

    protected $_tradeNo;
    protected $_paybill;

    public function __construct($trade_no, $paybill, $periodStart, $account) {
        $this->_tradeNo = $trade_no;
        $this->_paybill = $paybill;
        parent::__construct($periodStart, $account);
        $this->_fields = array(
            'transaction_number' => $trade_no,
            'trader' => 'backend-account-dr',
            'customer_id' => $paybill->AccountId,
            'member' => 'admin',
            'amount' => $paybill->Money,
            'currency' => 'CNY',
            'rate' => '1',
            'rate_amount' => '0',
            'period_start' => $periodStart,
            'period_end' => $account->Money,
            'business_type' => 'baozhengjing-dr',
            'order_sn' => ''
        );
    }

    protected function _getTrades() {
        return array(
            Application_Model_PayBill::PAY_PAYPAL => 'other_Paypal',
            Application_Model_PayBill::PAY_ALIPAY => 'other_alipay',
            Application_Model_PayBill::PAY_CARD => 'xinyongka',
            Application_Model_PayBill::PAY_ACCOUNT => 'backend-account-dr'
        );
    }

    protected function _addRechangeInfo() {
        $rechange = new Rechange($this->_tradeNo, $this->_paybill, $this->_periodStart, $this->_account);
        $rechange->addTransaction();
        $account = Application_Model_UserAccount::load($this->_account->id);
        $this->_periodStart = $account->Money;
        return $this;
    }

    public function addTransaction() {
        $trades = $this->_getTrades();
        $this->setFields('trader', $trades[$this->_paybill->payId]);

        $visiting = Application_Model_VisitingServer::load($this->_paybill->DomainId);
        if ($visiting) {
            if (0 == $visiting->isWay) { //进入云仓库
                $periodStart = $this->_periodStart;
                if ($this->_paybill->payId != Application_Model_PayBill::PAY_ACCOUNT) {
                    $this->_addRechangeInfo();
                    $periodStart = $this->_account->Money;
                }
                $user = Application_Model_User::load($this->_paybill->AccountId);
                $this->setFields('business_type', 'baozhengjing-dr')
                        ->setFields('amount', $visiting->guaranteeFee)
                        ->setFields('period_start', $periodStart)
                        ->setFields('period_end', $periodStart - $visiting->guaranteeFee)
                        ->setFields('customer_name', $user->nikename)
                        ->setFields('order_sn', $visiting->visitingNo);
                $url = $this->_buildUrl('addTransaction');

                $this->_updateMageFinance($url, $this->getFields());
            } else if (1 == $visiting->isWay) {
                $weybill = new Waybill($this->_tradeNo, $this->_paybill, $this->_periodStart, $this->_account);
                $weybill->setDomainId($visiting->wayId)
                        ->addTransaction();
            }
        }
        return $this;
    }

}
